﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace Chip8Dev.CPU
{
    public sealed partial class Interpreter : ICpuEngine
    {
        private void Opcode_LD_6(Instruction inst)
        {
            DebugWrite(String.Format("V{0:2X} = {1:2X}", inst.X, inst.KK));

            m_State.VRegisters[inst.X] = inst.KK;
        }

        private void Opcode_LD_8(Instruction inst)
        {
            DebugWrite(String.Format("V{0:2X} = V{1:2X}", inst.X, inst.Y));

            m_State.VRegisters[inst.X] = m_State.VRegisters[inst.Y];
        }

        private void Opcode_LD_A(Instruction inst)
        {
            DebugWrite(String.Format("I = {0:4X}", inst.NNN));

            m_State.Index = inst.NNN;
        }

        private void Opcode_F17(Instruction inst)
        {
            DebugWrite(String.Format("V{0:2X} = {1:2X}", inst.X, m_State.DelayTimerReg));

            m_State.VRegisters[inst.X] = m_State.DelayTimerReg;
        }

        private void Opcode_F0A(Instruction inst)
        {
            DebugWrite(String.Format("Wait for key", inst.X, inst.Y));
            /* TODO: Wait for key */
        }

        private void Opcode_F15(Instruction inst)
        {
            DebugWrite(String.Format("Delay Timer = V{0:2X}", inst.X));

            m_State.DelayTimerReg = m_State.VRegisters[inst.X];
            /* TODO: Set timer event */
        }

        private void Opcode_F18(Instruction inst)
        {
            DebugWrite(String.Format("Sound Timer = V{0:2X}", inst.X));
            /* TODO: Set delay timer */
        }

        private void Opcode_F1E(Instruction inst)
        {
            DebugWrite(String.Format("I = I + V{0:2X}", inst.X));

            m_State.Index += m_State.VRegisters[inst.X];
        }

        private void Opcode_F29(Instruction inst)
        {
            DebugWrite(String.Format("I = V{0:2X}", inst.X));

            m_State.Index = m_State.VRegisters[inst.X] * 8;
        }

        private void Opcode_F33(Instruction inst)
        {
            DebugWrite(String.Format("Store BCD of V{0:2X} in memory", inst.X));

            /* TODO: BCD store */
        }

        private void Opcode_F55(Instruction inst)
        {
            DebugWrite("Store regs in memory");

            for (int i = 0; i < inst.X; i++)
            {
                /* TODO: write to memory */
            }
        }

        private void Opcode_F65(Instruction inst)
        {
            DebugWrite("Restore regs from memory");
        }
    }
}
